package com.handpay.zztong.hp.main.model.discovery;

import android.content.Context;

import com.handpay.framework.ClientEngine;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.config.HttpUrlConstant;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.db.ormlite.DataBaseModel;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.notification.activity.NoticeActivity;
import com.handpay.zztong.hp.notification.activity.PushDetailActivity;
import com.handpay.zztong.hp.notification.bean.NoticeBody;
import com.handpay.zztong.hp.notification.bean.UserNoticesBean;
import com.handpay.zztong.hp.notification.model.NoticeModel;
import com.handpay.zztong.hp.other.activity.ShowHtmlActivity;
import com.handpay.zztong.hp.protocol.ATProtocolConstant;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.handpay.zztong.hp.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 * Created by fplei on 2017/5/10.
 */

public class DiscoveryModel {
//    private String[] titles = {"用户通知", "最新资讯", "即时消息"};
    private String[] titles = {"用户通知", "最新资讯"};
    private static final String NO_USERNOTICE = "暂无用户通知！";
    private static final String NO_NEWS = "暂无最新资讯！";
    private static final String NO_MESSAGE = "暂无即时推送消息！";
    private static DiscoveryModel discoveryModel = null;
    private DataBaseModel dataBaseModel = null;
    private Context mContext;
    private NoticeModel noticeModel = null;

    public static DiscoveryModel getInstance() {
        if (discoveryModel == null) {
            discoveryModel = new DiscoveryModel();
        }
        return discoveryModel;
    }

    public DiscoveryModel setContext(Context context) {
        this.mContext = context;
        dataBaseModel = DataBaseModel.getInstance(context);
        if (this.mContext instanceof ZZTongActivity) {
            noticeModel = new NoticeModel((ZZTongActivity) this.mContext);
        } else {
            HPLog.i(HPLog.TAG_RESULT, "---NoticeModel--init--fail");
        }
        return discoveryModel;
    }


    public ArrayList<DiscoveryModleBean> getItemDatas() {
        ArrayList<DiscoveryModleBean> diss = new ArrayList<DiscoveryModleBean>();
        for (int i = 0; i < titles.length; i++) {
            DiscoveryModleBean modleBean = new DiscoveryModleBean();
            switch (i) {
                case 0:
                    modleBean.setItem_icon(R.drawable.discovery_usernotice);
                    int temp_count = getNoReadCount();
                    modleBean.setIsNewFlag(temp_count > 0 ? temp_count : 0);
                    modleBean.setClazz(NoticeActivity.class);
                    modleBean.setPrepareField("");
                    break;
                case 1:
                    modleBean.setItem_icon(R.drawable.discovery_news);
                    modleBean.setIsNewFlag(0);
                    modleBean.setClazz(ShowHtmlActivity.class);
                    modleBean.setPrepareField("https://www.handmart.cn/web/LatestInfomation/html/NewInfomationList.html");
                    break;
                /*case 2:
                    modleBean.setItem_icon(R.drawable.discovery_message);
                    modleBean.setIsNewFlag(0);
                    modleBean.setClazz(PushDetailActivity.class);
                    modleBean.setPrepareField("");
                    break;*/
            }
            modleBean.setTag(i + "");
            modleBean.setItem_title(titles[i]);
            diss.add(modleBean);
        }
        return diss;
    }

    /**
     * 获取最后一条消息
     *
     * @return
     */
    public NoticeBody getLastNotice() {
        if (dataBaseModel == null || this.mContext == null) return null;
        NoticeBody noticeBody = dataBaseModel.getLastNotice(AccountUtils.getUserId(this.mContext));
        return noticeBody;
    }

    /**
     * 拉取消息
     *
     * @param listener
     */
    public void doGetNotices(BusinessEngineListener<UserNoticesBean> listener) {
        HPLog.i(HPLog.TAG_RESULT, "-----doGetNotices-----");
        if (dataBaseModel == null || noticeModel == null || listener == null) return;
        HPLog.i(HPLog.TAG_RESULT, "--UserId:" + AccountUtils.getUserId(mContext));
        String userId = AccountUtils.getUserId(mContext);
        String lastUpdateTime = PreferenceManager.getInstance(mContext).getString(userId, "");
        Hashtable<String, String> params = new Hashtable<>();
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
        params.put(ATProtocolConstant.CLIENTCHANNEL, ZZTConfig.UPDATE_CHANNEL);
        params.put(ATProtocolConstant.UPDATETIME, lastUpdateTime);
        noticeModel.getNotices(HttpUrlConstant.Do_GetUserNotices, params, listener);
    }

    //更新消息
    public void updateNotices(UserNoticesBean userNotices) {
        if (mContext == null || dataBaseModel == null) return;
        String lastUpdateTime = userNotices.getLastUpdateTime();
        String userId = AccountUtils.getUserId(mContext);
        PreferenceManager.getInstance(mContext).putString(userId, lastUpdateTime);
        if (userNotices.getList() != null && userNotices.getList().size() > 0) {
            for (NoticeBody noticeBody : userNotices.getList()) {
                noticeBody.setUserId(userId);
                noticeBody.setLastUpdateTime(lastUpdateTime);
                dataBaseModel.addNotice(noticeBody);
            }
        }
        HPLog.d(HPLog.SSX_TAG, "修改更新时间之前:" + dataBaseModel.queryNotices(userId).toString());
        dataBaseModel.updateLastTime(userId, lastUpdateTime);
    }

    public int getNoReadCount() {
        if (dataBaseModel == null || this.mContext == null) return 0;
        return dataBaseModel.queryNoReadCount(AccountUtils.getUserId(this.mContext));
    }


    public synchronized void onDestroy() {
        discoveryModel = null;
        dataBaseModel = null;
        noticeModel = null;
        this.mContext = null;
    }
}
